package com.yanqu.road.dao.impl.activity.xingyoushanshui.cross;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiBuildingLogData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossXingYouShanShuiBuildingLogDataDaoImpl extends TempDataObjectDao<CrossXingYouShanShuiBuildingLogData> {

    public long getMaxId(int activityId, int groupId) {
        String sql = "select max(id) as maxId from t_c_xing_you_team_build_log_data where activityId=? and groupId = ?";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setInt(i++,groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                long data = rs.getLong("maxId");
                if(data == 0){
                    return 1;
                }
                return data + 1;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return 1;
    }

    public static String getCrossXingYouShanShuiBuildingLogDataSql = "select * from `t_c_xing_you_team_build_log_data` where `activityId`=? and groupId = ?";
    public Map<Integer, List<CrossXingYouShanShuiBuildingLogData>> getCrossXingYouShanShuiBuildingLogData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<CrossXingYouShanShuiBuildingLogData>> resultMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossXingYouShanShuiBuildingLogDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossXingYouShanShuiBuildingLogData data = parse(rs);
                if(!resultMap.containsKey(data.getTeamId())){
                    resultMap.put(data.getTeamId(), new ArrayList<>());
                }
                resultMap.get(data.getTeamId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return resultMap;
    }

    public static String updateCrossXingYouShanShuiBuildingLogDataSql = "update `t_c_xing_you_team_build_log_data` set teamId=?,mapIndex=?,gridId=?,playerId=?,buildLv=?,isUseItem=?,logTime=? where activityId=? and groupId=? and id=?";
    @Override
    protected boolean updateData(CrossXingYouShanShuiBuildingLogData crossXingYouShanShuiBuildingLogData) {
//        crossXingYouShanShuiBuildingLogData.setBeginWriteTime();
//        getDb().setConnName(DbNameStrategy.getCrossDb());
//        int i = 1;
//        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
//        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiBuildingLogData.getTeamId()));
//        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiBuildingLogData.getMapIndex()));
//        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiBuildingLogData.getGridId()));
//        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiBuildingLogData.getPlayerId()));
//        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiBuildingLogData.getBuildLv()));
//        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiBuildingLogData.getIsUseItem()));
//        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiBuildingLogData.getLogTime()));
//        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiBuildingLogData.getActivityId()));
//        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiBuildingLogData.getGroupId()));
//        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiBuildingLogData.getId()));
//        if (getDb().executeNoneQuery(updateCrossXingYouShanShuiBuildingLogDataSql, para) > 0) {
//            return true;
//        }
        return false;
    }

    public static String insertCrossXingYouShanShuiBuildingLogDataSql = "insert into `t_c_xing_you_team_build_log_data`(activityId,groupId,id,teamId,mapIndex,gridId,playerId,buildLv,isUseItem,logTime) values (?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossXingYouShanShuiBuildingLogData crossXingYouShanShuiBuildingLogData) {
        crossXingYouShanShuiBuildingLogData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiBuildingLogData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiBuildingLogData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiBuildingLogData.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiBuildingLogData.getTeamId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiBuildingLogData.getMapIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiBuildingLogData.getGridId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiBuildingLogData.getPlayerId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiBuildingLogData.getBuildLv()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiBuildingLogData.getIsUseItem()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiBuildingLogData.getLogTime()));
        if (getDb().executeNoneQuery(insertCrossXingYouShanShuiBuildingLogDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossXingYouShanShuiBuildingLogData parseData(ResultSet rs) throws SQLException {
        CrossXingYouShanShuiBuildingLogData data = new CrossXingYouShanShuiBuildingLogData();
        data.setActivityId(rs.getInt("activityId"));
        data.setId(rs.getLong("id"));
        data.setTeamId(rs.getInt("teamId"));
        data.setMapIndex(rs.getInt("mapIndex"));
        data.setGridId(rs.getInt("gridId"));
        data.setPlayerId(rs.getLong("playerId"));
        data.setBuildLv(rs.getInt("buildLv"));
        data.setIsUseItem(rs.getInt("isUseItem"));
        data.setLogTime(rs.getLong("logTime"));
        data.setGroupId(rs.getInt("groupId"));

        return data;
    }
}
